<?php

$keyword = "";
$listaStanica = "";
$isAdmin = isAdmin();
?>
<div class='content'>
<form method='POST' action='stanice.php?action=pretraga'>
<table class='tableResult' style='width: 100%'>
	<tr>
		<th scope='col' class='spec' colspan='3'> Pretraga stanica </th>
		<th scope='col' class='spec' > Spremnik </th> 
		<th scope='col' class='spec' colspan='6' > Lokacija u spremniku </th>  
		<th scope='col' class='spec'></th>  
	</tr>
	<tr>
		<th scope='row' class='spec'>  </th>
		<td>
			<select name='keywordSelect'>
			<option value=''></option>";
			<?php printOptionsOfAtributes("stanica", "naziv");?>
			</select>
		</td>
		<td> 
			<input type='text' name='keyword' placeholder='Upišite riječ pretrage..'>
		</td>
		
		<td>
			<select name='stanicaSpremnik' id='stanicaSpremnik' style="width: 120px">
				<?php printOptionsOfAtributes("spremnik", "naziv") ?>
			</select>
		</td>
		
		<td>
			<select name='stanicaPolica' id='stanicaPolica' style="width: 40px">
				<option value=''></option>
				<?php for ($i=1; $i<=4; $i++) print("<option value='" . $i . "'>" . $i . "</option>"); ?>
			</select>
		</td>
		
		<td>
			<select name='stanicaRack' id='stanicaRack' style="width: 40px">
				<option value=''></option>
				<?php for ($i=1; $i<=4; $i++) print("<option value='" . $i . "'>" . $i . "</option>"); ?>
			</select>
		</td>
	
		<td>
			<select name='stanicaKolona' id='stanicaKolona' style="width: 40px">
				<option value=''></option>
				<?php for ($i='A'; $i<='F'; $i++) print("<option value='" . $i . "'>" . $i . "</option>"); ?>
			</select>
		</td>
		
		<td>
			<select name='stanicaLadica' id='stanicaLadica' style="width: 40px">
				<option value=''></option>
				<?php for ($i=1; $i<=9; $i++) print("<option value='" . $i . "'>" . $i . "</option>"); ?>
			</select>
		</td>

		<td> 
			<select name='stanicaRed' id='stanicaRed' style="width: 40px">
				<option value=''></option>
				<?php for ($i=1; $i<=9; $i++) print("<option value='" . $i . "'>" . $i . "</option>"); ?>
			</select>
		</td>
		
		<td>
			<select name='stanicaPolozaj' id='stanicaPolozaj' style="width: 40px">
				<option value=''></option>
				<?php for ($i=1; $i<=9; $i++) print("<option value='" . $i . "'>" . $i . "</option>"); ?>
			</select>
		</td>
		
		<td> 
			<input type='submit' value='Pretraži' >
		</td>
	</tr>
</table>
</form>
</div><br>


<?php 
if (isset($_POST['keyword']) || isset($_GET['keyword'])) {
	if (isset($_POST['keyword'])) $keyword = $_POST['keyword'];
	else $keyword = $_GET['keyword'];
	
	$keywordSelect = $_POST['keywordSelect'];
	
	$spremnik = $_POST['stanicaSpremnik'];
	$polica = $_POST['stanicaPolica'];
	$rack = $_POST['stanicaRack'];
	$kolona = $_POST['stanicaKolona'];
	$ladica = $_POST['stanicaLadica'];
	$red = $_POST['stanicaRed'];
	$polozaj = $_POST['stanicaPolozaj'];
	
	$pozicija = $polica;
	if (strlen($rack) != 0) $pozicija .= "-" . $rack;
	if (strlen($kolona) != 0) $pozicija .= "-" . $kolona;
	if (strlen($ladica) != 0) $pozicija .= "-" . $ladica;
	if (strlen($red) != 0) $pozicija .= "-" . $red;
	if (strlen($polozaj) != 0) $pozicija .= "-" . $polozaj;
	
	$query = "SELECT * FROM spremnik 
			INNER JOIN stanica ON stanica.spremnik=spremnik.id
			WHERE stanica.naziv LIKE '%" . $keywordSelect . "%' AND 
			(stanica.naziv LIKE '%" . $keyword . "%' OR vlasnik LIKE '%" . $keyword . "%') AND
			spremnik.naziv LIKE '%" . $spremnik . "%' AND pozicija LIKE '%" . $pozicija . "%';";
	
	$result = myQuery($query);
	
	$listaStanica .= "<div class='content'>
			<span class='contentTitle'> Lista stanica </span>
			<table class='tableResult' cellspacing='0' style='width: 100%'><tr>
			<th scope='col' width='200px'> Naziv </th> 
			<th scope='col' width='150px'> Spremnik </th>
			<th scope='col' width='100px'> Pozicija </th> 
			<th scope='col' width='100px'> Datum </th>
			<th scope='col' width='150px'> Vlasnik </th>";	
	
	while ($row = mysql_fetch_array($result)) {
		$listaStanica .= "<tr align=\"center\">
				<td> <a href=\"stanice.php?action=info&id=" . $row['id'] . "\"> " . $row['naziv'] . "</td>
				<td> " . dbGetNazivFromId("spremnik", $row['spremnik']) . "</td>
				<td> " . $row['pozicija'] . "</td>
				<td> " . $row['datum'] . "</td>
				<td> " . $row['vlasnik'] . "</td>";
		
		if ($isAdmin) $listaStanica .= "
				<td width='16px'> <a title='Promijeni stanicu' href='stanice.php?action=edit&type=stanica&id=" . $row['id'] . "'> 
					<img src='/images/edit.png' width='16px' height='16px'> </a> </td>
				<td width='16px'> <a title='Pobriši stanicu' href='exec/stanice.php?action=delete&type=stanica&id=" . $row['id'] . "&keyword=" . $keyword . "'> 
					<img src='/images/delete.png' width='16px' height='16px'> </td>";
		
		$listaStanica .= "</tr>";
	}
	
	$listaStanica .= "</table></div>";
}

print($listaStanica);

?>